# -*- coding: utf-8 -*-
# @Author	: starview.guojian.ma
# @Date		: 2021/12/3 17:09
# @Last Modified by:   starview.guojian.ma
# @Last Modified time: 2021/12/3 17:09
# Thanks for your comments!


import os
import yaml
import logging

logger = logging.getLogger(__name__)


def get_config_file_path():
    path_one = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
    file_name = os.path.join(path_one, "etc", "config.yaml")
    if os.path.exists(file_name):
        return file_name
    return "etc/config.yaml"


class Config(object):
    """
    配置
    """

    default_request_timeout = 300
    batch_size = 100
    server_start_sync_data = 1

    redis = {}
    mongodb = {}
    proxies = {}

    timer = {}

    # api配置
    api = {
        "host": "0.0.0.0",
        "port": 8080,
        "worker_connections": 1000
    }

    rabbitmq_xhhk = {}
    mq_config = {}

    def __init__(self, config_dir):
        self.load(config_dir)

    def load(self, config_dir):
        if os.path.isfile(config_dir):
            f = open(config_dir, encoding="utf-8")
            d = yaml.safe_load(f)

            for k, v in d.items():
                if hasattr(self, k):
                    if isinstance(v, dict):
                        for _k, _v in v.items():
                            self.__getattribute__(k)[_k] = _v
                    else:
                        self.__setattr__(k, v)


file_name = get_config_file_path()
logger.info("load config:{0}".format(file_name))

CONF = Config(file_name)

__all__ = ["CONF"]
